#include <stdio.h>
#include <unistd.h>
#include <sys/select.h>
#include<iostream>

void printfFD_set(fd_set * f,int maxFd)
    {
        for(int i=0;i<maxFd;i++)
        {
            if(FD_ISSET(i,f))
            {
                printf("%d ",i);
            }
        }
        std::cout<<"ready"<<std::endl;
    }

int main()
{
    fd_set read_fds;
    FD_ZERO(&read_fds);
    FD_SET(0, &read_fds);
    for (;;)
    {
        printfFD_set(&read_fds,1);
        printf("> ");
        fflush(stdout);
        int ret = select(1, &read_fds, NULL, NULL, NULL);
        std::cout<<"second prin:";
        printfFD_set(&read_fds,1);
        if (ret < 0)
        {
            perror("select");
            continue;
        }
        if (FD_ISSET(0, &read_fds))
        {
            char buf[1024] = {0};
            read(0, buf, sizeof(buf) - 1);
            printf("input: %s", buf);
        }
        else
        {
            printf("error! invaild fd\n");
            continue;
        }
        FD_ZERO(&read_fds);
        FD_SET(0, &read_fds);
    }
    return 0;
}